package cn.com.nwpu.visual_system.vo;

import cn.com.nwpu.visual_system.entity.*;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Hutengzhong
 * @version 1.0.0
 * @title ToVo
 * @description TODO 数据库数据格式转vo显示格式
 * @date 2024/4/27 17:45
 */
public class ToVo {
    public static List<ReactorVo> reactorListToVO(List<Reactor> reactorList) {
        List<ReactorVo> list = reactorList.stream()
                .map(entity -> new ReactorVo(entity.getReactorId(), entity.getReactorName()))
                .collect(Collectors.toList());
        return list;
    }

    public static List<ReactorDetailVo> toReactorDetailVoList(List<Sensor> sensorProcessList, List<SensorProcessData> sensorProcessDataList) {
        List<ReactorDetailVo> list = new ArrayList<>();

        SensorProcessData spd = sensorProcessDataList.get(0);
        ObjectMapper objectMapper = new ObjectMapper();
        Map<String, Object> sensorDataMap = objectMapper.convertValue(spd, Map.class);

        Double percentageValue;
        Double value;
        Double maxValue;
        Double minValue;

        for (Sensor sensor : sensorProcessList) {
            String sensorName = sensor.getSensorName().replaceAll("\\(|\\)", "").toLowerCase();

            maxValue = sensor.getMaxValue();
            minValue = sensor.getMinValue();
            value = Double.valueOf((sensorDataMap.get(sensorName).toString()));
            percentageValue = (value - minValue) / (maxValue - minValue) * 100;
            if (minValue.equals(maxValue))
                percentageValue = 50.0;
            ReactorDetailVo vo = new ReactorDetailVo();
            vo.setSensorId(sensor.getSensorId());
            vo.setSensorName(sensor.getSensorDetail());
            vo.setValue(value);
            vo.setPercentageValue(percentageValue);

            list.add(vo);
        }
        return list;
    }

    public static List<CurrentInfoVo> toCurrentInfoVo(List<CurrentInfo> currentInfoList) {
        List<CurrentInfoVo> list = new ArrayList<>();
        for (CurrentInfo cif : currentInfoList) {
            CurrentInfoVo civ = new CurrentInfoVo(cif.getCurrentReactorTemperature1(),
                    cif.getCurrentReactorTemperature2(),
                    cif.getCurrentReactorTemperature3());
            list.add(civ);
        }
        return list;
    }

    public static String[][][] toOperateDataVo(List<SensorOperateData> sensorOperateDataList) {
        String[][][] arrays = new String[4][5][];
        for (int i = 0; i < 4; ++i) {
            arrays[i] = new String[5][];
            for (int j = 0; j < 5; ++j) {
                arrays[i][j] = new String[11];
            }
        }
        {
            arrays[0][0][0] = "dataId";
            arrays[0][1][0] = "D进料量（流2）";
            arrays[0][2][0] = "E进料量（流3）";
            arrays[0][3][0] = "A进料量（流1）";
            arrays[0][4][0] = "总进料量（流4）";

            arrays[1][0][0] = "dataId";
            arrays[1][1][0] = "压缩机再循环阀";
            arrays[1][2][0] = "排放阀（流9）";
            arrays[1][3][0] = "分离器罐液流量（流10）";

            arrays[2][0][0] = "dataId";
            arrays[2][1][0] = "汽提器液体产品流量（流11）";
            arrays[2][2][0] = "汽提器水流阀";

            arrays[3][0][0] = "dataId";
            arrays[3][1][0] = "感应器冷却水流量";
            arrays[3][2][0] = "冷凝水冷却水流量";
        }

        int index = 1;  // 记录是第几条记录，对应到数组中的第几个
        // 多跑几次循环，每次数量级也不高，就10条
        for (SensorOperateData sod : sensorOperateDataList) {
            ObjectMapper objectMapper = new ObjectMapper();
            Map<String, Object> sensorDataMap = objectMapper.convertValue(sod, Map.class);
            arrays[0][0][index] = sensorDataMap.get("sensorDataId").toString();
            arrays[0][1][index] = sensorDataMap.get("xmv1").toString();
            arrays[0][2][index] = sensorDataMap.get("xmv2").toString();
            arrays[0][3][index] = sensorDataMap.get("xmv3").toString();
            arrays[0][4][index] = sensorDataMap.get("xmv4").toString();

            arrays[1][0][index] = sensorDataMap.get("sensorDataId").toString();
            arrays[1][1][index] = sensorDataMap.get("xmv5").toString();
            arrays[1][2][index] = sensorDataMap.get("xmv6").toString();
            arrays[1][3][index] = sensorDataMap.get("xmv7").toString();

            arrays[2][0][index] = sensorDataMap.get("sensorDataId").toString();
            arrays[2][1][index] = sensorDataMap.get("xmv8").toString();
            arrays[2][2][index] = sensorDataMap.get("xmv9").toString();

            arrays[3][0][index] = sensorDataMap.get("sensorDataId").toString();
            arrays[3][1][index] = sensorDataMap.get("xmv10").toString();
            arrays[3][2][index] = sensorDataMap.get("xmv11").toString();
            index++;
        }
        return arrays;
    }
}
